package com.souku.finance.application.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RegisterBean;
import com.souku.finance.application.ui.MainActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.db.AvatarUtil;
import com.souku.finance.application.ui.mine.MineFragment;
import com.souku.finance.application.ui.more.ForgetLoginPassActivity;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.utils.ViewUtil;
import com.souku.finance.application.widget.ButtonWait;
import com.souku.finance.application.widget.RoundImageView;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.RequestSuccessError;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Android on 2016/12/7.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected RoundImageView ivHead;
    protected EditText etMobile;
    protected EditText etPass;
    protected TextView btnRegister;
    protected ButtonWait btnLogin;
    protected ImageView image;
    protected ImageView image1;
    protected ImageView ivDeletPhone;
    protected ImageView ivDeletPass;
    protected Button btnForget;
    protected LinearLayout contentview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        initView();
        ViewUtil.setDelete(etMobile, ivDeletPhone, null);
        ViewUtil.setDelete(etPass, ivDeletPass, null);
        AvatarUtil.ivHeadListenr(this, etMobile, ivHead);
    }

    @Override
    public void internet() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        String mobile = ShareUtil.getInstance().getString(Constant.Mobile);
        if (mobile != null) {
            etMobile.setText(mobile);
            etMobile.setSelection(etMobile.getText().length());
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_register) {
            Intent intent = new Intent(this, Register2Activity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_login) {
            String mobile = etMobile.getText().toString().trim();
            if (mobile.isEmpty()) {
                ToastUtil.show("请输入手机号");
                return;
            }
            if (!MyTextUtil.isTelephone(mobile)) {
                ToastUtil.show("请输入正确的手机号");
                return;
            }
            String password = etPass.getText().toString().trim();
            if (password.isEmpty()) {
                ToastUtil.show("请输入密码");
                return;
            }
            if (password.length() < 8) {
                ToastUtil.show("密码长度8-20位");
                return;
            }
            MobclickAgent.onEvent(LoginActivity.this,"login");
            login(mobile, password);
        } else if (view.getId() == R.id.top_back) {
            finish();
        } else if (view.getId() == R.id.btn_forget) {
            Intent intent = new Intent(this, ForgetLoginPassActivity.class);
            startActivity(intent);
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(LoginActivity.this);
        ivHead = (RoundImageView) findViewById(R.id.iv_head);
        etMobile = (EditText) findViewById(R.id.et_mobile);
        etPass = (EditText) findViewById(R.id.et_pass);
        btnRegister = (TextView) findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(LoginActivity.this);
        btnLogin = (ButtonWait) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(LoginActivity.this);
        image = (ImageView) findViewById(R.id.image);
        image1 = (ImageView) findViewById(R.id.image1);
        ivDeletPhone = (ImageView) findViewById(R.id.iv_delet_phone);
        ivDeletPass = (ImageView) findViewById(R.id.iv_delet_pass);
        btnForget = (Button) findViewById(R.id.btn_forget);
        btnForget.setOnClickListener(LoginActivity.this);
        contentview = (LinearLayout) findViewById(R.id.contentview);
    }

    private void login(String mobile, String password) {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("mobile", mobile);
        map.put("userPwd", password);
        map.put("lon", "");
        map.put("lat", "");
        String pushId = JPushInterface.getRegistrationID(this);
        if (!MyTextUtil.isEmpty(pushId)) {
            map.put("pushToken", pushId);
        }
        RequestManager.getInstance().post(UrlConstact.LOGIN, ParamUtil.getSing(map), new ReqListener(this, new RequestSuccessError() {
            @Override
            public void onRequest() {
                btnLogin.setEnabled(false);
            }

            @Override
            public void onSuccess(String response) {
                btnLogin.setEnabled(true);
                if (JsonUtil.getCode(response) == 1) {
                    RegisterBean bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), RegisterBean.class);
                    //保存头像url到SQL
                    AvatarUtil.insert(LoginActivity.this, bean.getMobile(), bean.getAvatarUrl());
                    //保存登录信息
                    ShareUtil.getInstance()
                            .putExtra(Constant.IsRegistered, true)
                            .putExtra(Constant.Mobile, bean.getMobile())
                            .putExtra(Constant.Token, bean.getOauthToken())
                            .putExtra(Constant.UserName, bean.getUserName())
                            .putExtra(Constant.UserId, bean.getId())
                            .putExtra(Constant.AvatarUrl, bean.getAvatarUrl())
                            .putExtra(Constant.IsNewHand, "1".equals(bean.getNewHandStatus()))
                            .putExtra(Constant.IsReal, "1".equals(bean.getRealStatus()))
                            .putExtra(Constant.IsRole, "1".equals(bean.getRoleType()))
                            .putExtra(Constant.IsSetPayPwd, "1".equals(bean.getPayPwdStatus()))
                            .putExtra(Constant.IsBind, bean.getIsBind())
                            .putExtra(Constant.IsBorrower, "1".equals(bean.getIsBorrower()))
                            .putExtra(Constant.IsLogin, true)
                            .putExtra(Constant.MemberLevel, bean.getMemberLevel())
                            .putExtra(Constant.CumulatedPoint, bean.getCumulatedPoint())
                            .putExtra(Constant.AvailablePoint, bean.getAvailablePoint())
                            .putExtra(Constant.MaxPoint, bean.getMaxPoint())
                            .putExtra(Constant.eAccountNum, bean.geteAccountNum())
                            .putExtra(Constant.eAccountBindTime, bean.geteAccountBindTime())
                            .putExtra(Constant.realName, bean.getRealName());
                    MainActivity.page = 2;
                    MineFragment.isChange = true;
//                    Intent intent = new Intent(LoginActivity.this, RegisterSucceedActivity.class);
//                    startActivity(intent);
                    finish();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                btnLogin.setEnabled(true);
//                ToastUtil.show("请检查网络");
            }
        }), ParamUtil.getHeaders());
    }

}
